package com.datang.petstore.controller.web;

import com.datang.petstore.entity.UserInfo;
import com.datang.petstore.model.SummaryOpenSourceResponse;
import com.datang.petstore.model.SummaryUserAgeResponse;
import com.datang.petstore.model.SummaryUserSexResponse;
import com.datang.petstore.model.getUserLoginSummaryResponse;
import com.datang.petstore.resultpage.Result;
import com.datang.petstore.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-11 11:20
 * @notify 用户相关api
 * @version 1.0
 */
@RestController
public class WebUserController {

    @Autowired
    private UserService userService;


    /*
     * @author 顶风少年
     * @date 2019/11/15 6:29
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param httpSession
     * @result com.datang.petstore.resultpage.Result
     * @notify 获取session
     * version 1.0
     */
    @GetMapping("/manage/user")
    public Result get(HttpSession httpSession) {
        Object username = httpSession.getAttribute("username");
        return Result.ok(username);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 6:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result com.datang.petstore.resultpage.Result
     * @notify 退出登录
     * version 1.0
     */
    @GetMapping("/manage/out")
    public Result out(HttpSession httpSession) {
        httpSession.removeAttribute("username");
        return Result.ok();
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 6:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result com.datang.petstore.resultpage.Result
     * @notify 用户性别汇总
     * version 1.0
     */
    @GetMapping("/manage/summary/sex")
    public Result getSummaryUserSex() {
        List<SummaryUserSexResponse> summaryUserSex = userService.getSummaryUserSex();
        return Result.ok(summaryUserSex);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/15 6:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result com.datang.petstore.resultpage.Result
     * @notify 用户年龄汇总
     * version 1.0
     */
    @GetMapping("/manage/summary/age")
    public Result getSummaryUserAge() {
        List<SummaryUserAgeResponse> summaryUserAge = userService.getSummaryUserAge();
        return Result.ok(summaryUserAge);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/15 6:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result com.datang.petstore.resultpage.Result
     * @notify 用户登录来源汇总
     * version 1.0
     */
    @GetMapping("/manage/summary/source")
    public Result getSummaryOpenSource() {
        List<SummaryOpenSourceResponse> summaryOpenSource = userService.getSummaryOpenSource();
        return Result.ok(summaryOpenSource);
    }


    /*
     * @author 顶风少年
     * @date 2019/11/15 6:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result com.datang.petstore.resultpage.Result
     * @notify 用户七日登录汇总
     * version 1.0
     */
    @GetMapping("/manage/summary/login")
    public Result getUserLoginSummary() {
        List<getUserLoginSummaryResponse> summaryOpenSource = userService.getUserLoginSummary();
        return Result.ok(summaryOpenSource);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/18 11:22
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param page
     * @result java.util.List<com.datang.petstore.entity.UserInfo>
     * @notify 分页获取用户列表
     * version 1.0
     */
    @GetMapping("/manage/user/list")
    public Result getUserInfoList(PageInfo page) {
        PageInfo userInfoList = userService.getUserInfoList(page);
        return Result.ok(userInfoList);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/18 16:39
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userInfo
     * @result void
     * @notify 更改用户信息
     * version 1.0
     */
    @PostMapping("/manage/userinfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo) {
        userService.updateUserInfo(userInfo);
        return Result.ok();
    }
}
